#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename	:  01-yum_update.sh
# Version	:  2.0
# Date		:  2024/04/23
# Author	:  yaoxiyao
# Email		:  yaoxiyao@kylinsec.com.cn
# Function	:  软件更新测试
# Out		:
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

# 测试主题
Title_Env_LTFLIB="软件更新测试"

## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB() {
    # 创建临时文件和目录
    test_update="${TmpTestDir_LTFLIB}/test-update"
    CommRetParse_FailDiy_LTFLIB ${ERROR} "创建文件失败${test_update}"
    test_example="${TmpTestDir_LTFLIB}/test-update-example"
    CommRetParse_FailDiy_LTFLIB ${ERROR} "创建文件失败${test_example}"
    return $TPASS
}

## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB() {
    return $TPASS
}

## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB() {
    testcase_1
    return $TPASS
}

## TODO : 更新软件包
testcase_1() {
    yum check-update | grep "$(arch)" >"${test_update}"
    if [[ -s "${test_update}" ]]; then
        cat "${test_update}" | awk 'NR==1' >"${test_example}"
        local update_name=$(cat "${test_example}" | awk -F'.' '{print $1}')
        local version=$(cat "${test_example}" | awk -F' ' '{print $2}' | cut -d ':' -f 2-)
        yum update "${update_name}" -y
        local version_sub2=$(rpm -qa | grep "${update_name}-" | awk 'NR==1' | awk -F- '{print $2}')
        local version_sub3=$(rpm -qa | grep "${update_name}-" | awk 'NR==1' | awk -F- '{print $3}' | sed "s/\.$(arch)$//")
        local update_version="${version_sub2}-${version_sub3}"
        Info_LLE "${update_version}"
        Info_LLE "${version}"
        if [[ "${update_version}" == "${version}" ]]; then
            OutputRet_LTFLIB ${TPASS}
            TestRetParse_LTFLIB "yum update ${update_name} -y"
        else
            OutputRet_LTFLIB ${TFAIL}
            TestRetParse_LTFLIB "yum update ${update_name} -y"
        fi
        yum remove "${update_name}" -y
    else
        OutputRet_LTFLIB ${TCONF}
        TestRetParse_LTFLIB "此源无更新包"
    fi
}

#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
